# To change this template, choose Tools | Templates
# and open the template in the editor.
class VecUtils
  # Vector multiplier
def VecUtils.mul_sum (vector_x,vector_y)
    mult= 0

    (0..vector_x.size-1).each{|i|
      mult= mult+ vector_x[i]*vector_y[i]
    }

    return mult
  end
  def VecUtils.sum(vector)
    return vector.inject{ |sum, el| sum + el }.to_f
  end
  # Trasforma il tutto in un vettore logaritmico
  def VecUtils.logarizzatore(vector)
    vec_log=[]

    vector.each{|x|
      vec_log.push(Math.log(x))
    }

    return vec_log
  end
  # Trasforma la matrice in un vettore. I valori del vettore sono
  # la media dei valori della matrice
  def VecUtils.matrix_av_vector(matrice)
    vector= []

    (0..matrice[0].size-1).each{ |i|
      average= 0
      matrice.each{|me|
        average= average + me[i]
      }
      average= average.to_f/(matrice.size)
      vector.push(average)
    }

    return vector
  end
  # Moltiplicazione vettori
  # Calcolo del valore medio del vettore
  def VecUtils.average(vector)
    return vector.inject{ |sum, el| sum + el }.to_f / vector.size
  end
  # Trova i primi n elementi più grandi
    def VecUtils.far(vector,num)
    far=[]
    max_val=[]
    (0..num-1).each{|i|
      max_val.push(vector.sort{|a,b| b<=>a}[i])
    }
    p max_val

    max_val.each{|x|
      if far.include?(vector.index(x)) then
        tmp=[]
        (far.last+1..vector.size-1).each{|i|
        tmp.push(vector[i])

        }
         far.push(tmp.index(x)+far.last+1)
      elsif
        far.push(vector.index(x))
      end
  }

    return far
  end
    # Trova i primi n elementi più piccoli
    def VecUtils.near(vector,num)
    near=[]
    min_val=[]
    (0..num-1).each{|i|
      min_val.push(vector.sort{|a,b| a<=>b}[i])
    }


    min_val.each{|x|
      if x!=0then
        if near.include?(vector.index(x))
        tmp=[]
        (near.last+1..vector.size-1).each{|i|
        tmp.push(vector[i])

        }
         near.push(tmp.index(x)+near.last+1)
      elsif
        near.push(vector.index(x))
      end
      end
  }

    return near
  end
end
